package utils

import (
	"context"
	_const "github.com/flipped-aurora/gin-vue-admin/server/global/const"
	"github.com/gin-gonic/gin"
	"github.com/rs/xid"
)

// GetLogID 从context中获取log_id; 若没有设置log_id，则生成一个uuid,并设置到ctx中
func GetLogID(c *gin.Context) string {
	var ctx = c.Request.Context()
	var value = ctx.Value(_const.LogIDKey)
	if value != nil {
		if logID, ok := value.(string); ok {
			return logID
		}
	}

	var uuid = xid.New().String()
	ctx = context.WithValue(ctx, _const.LogIDKey, uuid)
	c.Request.WithContext(ctx)

	return uuid
}
